/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.audit.service  
 */
package com.apache.audit.service.plugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.auditRule.entity.Audit;
import com.apache.auditRule.entity.AuditRule;
import com.apache.auditRule.manager.AuditManager;
import com.apache.auditRule.server.AuditSaveHelper;
import com.apache.cache.util.Validator;
import com.apache.member.common.ConstantData;
import com.apache.tools.ClassToolsUtil;

/**
 * description:  审核公共方法插件
 * @author cbqiu 创建时间：2016-7-21  
 */
public class AuditSavePluginImpl implements PluginConnector {

	private Logger log = Logger.getLogger(AuditSavePluginImpl.class);
	private AuditManager auditManager;

	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		try {
			result.setResult("true");
			//如果methodKey为init，则代表了初始化，如果不是init并且不为空，则为审核方法。
			String methodKey = String.valueOf(vo.getParams("methodKey"));
			String auditFlag = String.valueOf(vo.getParams("auditFlag"));//T

			Audit auditObj = new Audit();
			auditObj = (Audit) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), auditObj);

			String auditId = auditObj.getAuditsId();

			if (!"init".equals(methodKey) && Validator.isNotNull(auditId)) {

				try {
					ParamsVo<Audit> auditVo = new ParamsVo<Audit>();
					vo.setInfoId(auditId);
					vo.setParams("pid", auditObj.getPid());
					Audit audit = (Audit) auditManager.getInfoById(vo);
					if (!Validator.isEmpty(audit)) {
						String status = audit.getAuditsStatus();
						//判断当前审核项状态是否还在审核中
						if (Validator.isNotNull(status) && ConstantData.AuditStatus.AUDITING.equals(status)) {
							//设置当前审核人
							audit.setCheckUser(String.valueOf(vo.getParams("checkUser")));
							audit.setUpdateUserEname(String.valueOf(vo.getParams("checkUser")));
							audit.setAuditOpinion(auditObj.getAuditOpinion());
							audit.setTaskName(auditObj.getTaskName());
							audit.setProId(auditObj.getProId());
							vo.setObj(audit);
							if (!Validator.isEmpty(audit)) {
								if (Validator.isNotNull(methodKey)) {
									vo.setMethodKey(methodKey);
								} else {
									vo.setMethodKey("pro-audit");
								}
								vo.setParams("auditFlag", auditFlag);
								if (auditManager.editInfo(vo)) {
									result.setMessage("修改操作成功！");
									result.setEntity(auditId);
								}
							}
						} else {
							result.setMessage("该记录的当前审核阶段已审核,将跳转到审核列表页面!");
						}
					}
				} catch (RuntimeException e) {
					e.printStackTrace();
				}

			} else {
				AuditRule auditRule = new AuditRule();
				auditRule.setTableName(auditObj.getTableName());
				auditRule.setAuditType(String.valueOf(vo.getParams("auditType")));
				auditRule.setCheckUser(String.valueOf(vo.getParams("checkUser")));
				vo.setObj(auditRule);
				AuditSaveHelper.newIntance().auditSave(vo);//插入操作
				result.setMessage("插入操作成功！");
				result.setEntity(auditId);
			}

		} catch (Exception e) {
			result.setResult("false");
			result.setMessage("执行失败！");
		}
		return result;
	}

	public void setAuditManager(AuditManager auditManager) {
		this.auditManager = auditManager;
	}

}
